package com.example.demo.controller.admin;

import com.example.demo.model.CategoryModel;
import com.example.demo.model.Json;
import com.example.demo.model.LabelModel;
import com.example.demo.services.LabelService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/back/label")
@RestController
public class LabelController {

    @Resource
    LabelService labelService;

    @RequestMapping("list")
    public ModelAndView list(
            Model model,
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize
    ){
        PageInfo pageInfo = labelService.getLabelListFromDb(page, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return new ModelAndView("admin/label/list");
    }

    @RequestMapping("search")
    public Json search(
            @RequestParam(name = "keyword", required = false, defaultValue = "") String keyword
    ){
        List<LabelModel> labelList = labelService.getLabelListFromDb(keyword);
        return new Json().success(labelList);
    }

    @RequestMapping("add")
    public ModelAndView add(
            Model model
    ){

        return new ModelAndView("admin/label/add");
    }

    @RequestMapping("addPost")
    public Json addPost(
            @RequestBody LabelModel labelModel
    )
    {
        try{
            int r = labelService.add(labelModel);
            if(r == 1) {
                return new Json().success("新增成功");
            } else {
                return new Json().error("新增失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

    @RequestMapping("edit")
    public ModelAndView edit(
            Model model,
            @RequestParam(name = "id", required = true) Long id
    ){
        LabelModel labelInfo = labelService.getLabelInfo(id);
        model.addAttribute("info", labelInfo);
        return new ModelAndView("admin/label/edit");
    }

    @RequestMapping("editPost")
    public Json editPost(
            @RequestBody LabelModel labelModel
    ){
        try{
            boolean r = labelService.edit(labelModel);
            if(r) {
                return new Json().success("修改成功");
            } else {
                return new Json().error("修改失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

    @RequestMapping("del")
    public Json del(
            @RequestBody LabelModel labelModel
    ){
        try{
            boolean r = labelService.del(labelModel.getId());
            if(r) {
                return new Json().success("删除成功");
            } else {
                return new Json().error("删除失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

}
